package nuitInfo.android;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.types.Post;
import com.restfb.types.User;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Filterable;
import android.widget.ListAdapter;
import android.widget.ListView;



public class ListFriendsActivity extends Activity {
	
	private static Context context;
	private static String token;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	context = this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_friend);
        
        // retrieve Access Token
        Intent thisIntent = getIntent();
        token = thisIntent.getExtras().getString("token");

        FacebookClient facebookClient = new DefaultFacebookClient(token);

        
        com.restfb.Connection<User> myFriends = facebookClient.fetchConnection("me/friends", User.class);

        System.out.println("Count of my friends: " + myFriends.getData().size());
        
        //get List view
        final ListView list = (ListView)findViewById(R.id.listView1);
        
        // populate list
        List<String> friendsNames = new ArrayList<String>();
        final List<User> friendsList = myFriends.getData();
        //Collections.sort(friendsList);
        
        for (int i = 0 ; i < myFriends.getData().size() ; i++) {
        	User u = friendsList.get(i);
        	System.out.println(u.getName());
        	friendsNames.add(u.getName());
        }
        
        
        final ListAdapter adapter = new ArrayAdapter<String>(this, R.layout.list_item, friendsNames);
        list.setAdapter(adapter);
        
        list.setClickable(true);        
        list.setTextFilterEnabled(true);
        
        //get Edit Text view
        EditText edit = (EditText)findViewById(R.id.editText1);
        
        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
                ((Filterable) adapter).getFilter().filter(arg0);
            }
        });


    
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        	@Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        		
        		// search facebook ID from name
        		String name = "";
        		String idFb = "";
        		for (int i = 0 ; i < friendsList.size() ; i++) {
        			User u = friendsList.get(i);
        			if (u.getName().equals(list.getItemAtPosition(position))) {
        				System.out.println(list.getItemAtPosition(position) + u.getName());
        				name = u.getName();
        				idFb = u.getId();
        				break;
        			}
        		}
            	System.out.println("nom : " + name + "position : " + position + "id : " + id +idFb +"\n");
            	
            	System.out.println("Token : " + token);
            	
            	Intent intent = new Intent(context, 
						GiftAndroidJSONActivity.class); 
				
				intent.putExtra("token", token);
				intent.putExtra("id", idFb);
				startActivity(intent);
				
            }

        });

        

    }
}